package com.dhf.framework.system.controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dhf.framework.base.mybatis.core.page.Pagination;
import com.dhf.framework.common.BaseController;
import com.dhf.framework.system.entity.SysMenu;
import com.dhf.framework.system.service.SysMenuService;

 /**
 * </b>SysMenuController<br>
 * @author MY
 * @version:1.0
 */
@Controller
@RequestMapping("/system/sysMenu")
public class SysMenuController extends BaseController{
		private static Logger LOGGER = LoggerFactory.getLogger(SysMenuController.class);
	
  		@Autowired
  		private SysMenuService sysMenuService;
  		
  		/**
		  * 
		  * @Title: queryPageList
		  * @Description: 列表页面
		  * @param @param searchEntity
		  * @param @param pagination
		  * @return ModelAndView    返回类型
		  * @throws
		*/
		@RequestMapping(value = "/list")
		public ModelAndView list(@ModelAttribute SysMenu sysMenu, Pagination<SysMenu> pagination) {
			LOGGER.debug("开始查询数据！");
			ModelAndView mv = new ModelAndView( "system/sysMenu/sysMenuList" );
			List<SysMenu> menuList = this.sysMenuService.listAll();
            mv.addObject("menuList", menuList);
			return mv;
		}
		
		/**
	     * 获取当前菜单的所有子菜单
	     * @param menuId
	     * @param response
	     */
	    @RequestMapping(value="/sub")
	    @ResponseBody
	    public List<SysMenu> getSub(@RequestParam String parentId){
            List<SysMenu> subMenu = this.sysMenuService.listSubMenuByParentId(parentId);
            return subMenu;
	    }
		
		
		/**
		 * 
		 * @Title: toMerge
		 * @Description: 新增或修改页面
		 * @param @param entity
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/toMerge")
		public ModelAndView toMerge(Long menuId){
			ModelAndView mv = new ModelAndView("system/sysMenu/sysMenuMerge");
			if(menuId != null){
				SysMenu sysMenu =  this.sysMenuService.get( menuId ) ;
				mv.addObject("sysMenu",sysMenu);
			}else{
				mv.addObject("sysMenu", new SysMenu());
			}
			List<SysMenu> menuList = this.sysMenuService.listAll();
			mv.addObject("menuList", menuList);
			return mv;
		}
		
		/**
	     * 请求编辑菜单图标页面
	     * @param 
	     * @return
	     */
	    @RequestMapping(value="/toEditicon")
	    public ModelAndView toEditicon(String menuId)throws Exception{
	        ModelAndView mv = new ModelAndView();
            mv.addObject("menuId", menuId);
            mv.setViewName("system/sysMenu/sysMenuIcon");
	        return mv;
	    }
		

		/**
		 * 
		* @Title: doMerge
		* @Description: 新增或修改数据 
		* @param @param request
		* @param @param entity
		* @return ModelAndView    返回类型
		* @throws
		 */
		@RequestMapping(value = "/doMerge")
		public ModelAndView doMerge(SysMenu entity,RedirectAttributes redirectAttributes) {
			if (entity.getMenuId()!=null) {
				LOGGER.debug("开始更新数据！");
				entity.setModifyTime(new Date());
				this.sysMenuService.update( entity );
			    this.addMessage(redirectAttributes, "修改数据成功");
			} else {
				LOGGER.debug("开始新增数据！");
				entity.setCreateTime(new Date());
				this.sysMenuService.add( entity );
				this.addMessage(redirectAttributes, "新增据成功");
			}
			return new ModelAndView( "redirect:/system/sysMenu/list.do" );
		}

		/**
		 * 
		 * @Title: delete
		 * @Description:删除数据
		 * @param @param id
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/delete")
		@ResponseBody
		public String delete(Long menuId,String flag,RedirectAttributes redirectAttributes) {
			LOGGER.debug("开始删除数据！");
			if("1".equals(flag)){
			    this.sysMenuService.delete( menuId );
			}else if("0".equals(flag)){
			    this.sysMenuService.delete( menuId );
			    this.sysMenuService.deleteByParentId(menuId);
			}
			return "删除数据成功!";
		}
}

